import { createRouter, createWebHistory } from 'vue-router'
import { createPinia } from 'pinia'
import { useUserStore } from '@/stores/user'

const pinia = createPinia()

const routes = [
  {
    path: '/',
    redirect: '/login',
    meta: { requiresAuth: false }
  },
  {
    path: '/dashboard',
    name: 'Dashboard',
    component: () => import('@/views/Dashboard.vue'),
    meta: {
      title: '系统首页',
      requiresAuth: true
    }
  }
  ,
  {
    path: '/profile',
    name: 'profile',
    component: () => import('@/views/UserCenter.vue'),
    meta: {
      title: '个人中心',
      requiresAuth: true
    }
  },
  {
    path: '/settings',
    name: 'settings',
    component: () => import('@/views/SystemSettings.vue'),
    meta: {
      title: '系统设置',
      requiresAuth: true
    }
  },
  {
    path: '/stock',
    name: 'Stock',
    component: () => import('@/views/Products.vue'),
    meta: {
      title: '库存管理',
      requiresAuth: true
    }
  }
  ,
  {
    path: '/user',
    name: 'UserManagement',
    component: () => import('@/views/UserManagement.vue'),
    meta: {
      title: '人员管理',
      requiresAuth: true
    }
  },
  {
    path: '/system',
    name: 'System',
    meta: {
      title: '系统管理',
      requiresAuth: true
    },
    children: [
      {
        path: '/system/workflow',
        name: 'User',
        component: () => import('@/views/WorkflowManagement.vue'),
        meta: {
          title: '流程管理',
          requiresAuth: true
        }
      },
      {
        path: '/system/permission',
        name: 'Role',
        component: () => import('@/views/PermissionManagement.vue'),
        meta: {
          title: '权限管理',
          requiresAuth: true
        }
      }
    ]

  }
  ,
  {
    path: '/login',
    name: 'Login',
    component: () => import('@/views/Login.vue'),
    meta: {
      title: '登录',
      requiresAuth: false,
      hideLayout: true
    }
  },
  {
    path: '/register',
    name: 'Register',
    component: () => import('@/views/Register.vue'),
    meta: {
      title: '注册',
      requiresAuth: false,
      hideLayout: true
    }
  },
  {
    path: '/403',
    name: 'Forbidden',
    component: () => import('@/views/error/403.vue'),
    meta: {
      title: '无权限访问',
      requiresAuth: false,
      hideLayout: true
    }
  },
  {
    path: '/404',
    name: 'NotFound',
    component: () => import('@/views/error/404.vue'),
    meta: {
      title: '页面不存在',
      requiresAuth: false,
      hideLayout: true
    }
  }
]

const dynamicRoutes = [
  {
    path: '/products',
    name: 'Products',
    component: () => import('@/views/Products.vue'),
    meta: {
      title: '商品管理',
      requiresAuth: true,
      permissions: ['product:manage']
    }
  }
]

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    ...routes,
    ...dynamicRoutes,
    {
      path: '/:pathMatch(.*)*',
      redirect: '/404'
    }
  ]
})

router.beforeEach((to, from, next) => {
  console.log('当前路径:', to.path);
  console.log('匹配的路由记录:', to.matched);
  next();
})

export default router